package cn.edu.tjnu.dao.impl;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import cn.edu.tjnu.dao.IChargeInfoDao;
import cn.edu.tjnu.myUtils.MyUtils;
import cn.edu.tjnu.orm.ChargeBean;

public class ChargeInfoDaoImpl implements IChargeInfoDao {

	private SessionFactory sessionFactory;

	@Override
	public List<ChargeBean> queryAllChargeInfo(ChargeBean chargeB) {

		String userIDCard = chargeB.getUserIDCard();
		Session session = sessionFactory.getCurrentSession();
		if (userIDCard.equals("")) {
			String sHql = "from ChargeBean where  userIDCard ='-1'";
			Query query = session.createQuery(sHql);
			List<ChargeBean> listCharge = query.list();
			return listCharge;
		} else {
			String sHql = "from ChargeBean  WHERE chargeNum =(SELECT MAX(chargeNum) FROM ChargeBean WHERE userIDCard=" + userIDCard+")";
			Query query = session.createQuery(sHql);
			List<ChargeBean> listCharge = query.list();
			return listCharge;
		}
		
	}
	public List<ChargeBean> queryShowChargeInfo(ChargeBean chargeB) {

		String userIDCard = chargeB.getUserIDCard();
		Session session = sessionFactory.getCurrentSession();
		if (userIDCard.equals("")) {
			String sHql = "from ChargeBean where  userIDCard ='-1'";
			Query query = session.createQuery(sHql);
			List<ChargeBean> listCharge = query.list();
			return listCharge;
		} else {
			String sHql = "from ChargeBean  WHERE userIDCard=" + userIDCard+"ORDER BY chargeNum";
			Query query = session.createQuery(sHql);
			List<ChargeBean> listCharge = query.list();
			return listCharge;
		}
		
	}
	@Override
	public int saveChargeInfo(ChargeBean chargeB) {
		Session session = sessionFactory.getCurrentSession();
		// 使用hibernate保存实体对象的数据
		Date  date = new  Date();  
		chargeB.setChargePayDate(date);
		chargeB.setChargerSumMoney(chargeB.getChargePayMoney()+chargeB.getChargerSumMoney());
		session.saveOrUpdate(chargeB);
		return 1;
		
	}
	@Override
	public int saveConsumeChargeInfo(ChargeBean chargeB) {
		Session session = sessionFactory.getCurrentSession();
		// 使用hibernate保存实体对象的数据
		Date  date = new  Date();  
		chargeB.setChargeConsumeDate(date);
		chargeB.setChargerSumMoney(chargeB.getChargerSumMoney()-chargeB.getChargeConsumeMoney());
		session.saveOrUpdate(chargeB);
		return 1;
	}
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	



}
